<template>
  <div>
    <UpdateExcel :on-success="onSuccess" />
  </div>
</template>

<script>
import { batchImportEmployees } from '@/api/employees'
import { formatDate } from '@/utils'

export default {
  name: 'EmployeesImport',
  data() {
    return {}
  },
  created() {

  },
  methods: {
    async onSuccess({ results }) {
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber'
      }
      const excelArr = results.map(item => {
        const obj = {}
        Object.keys(item).forEach(key => {
          if (key === '入职日期' || key === '转正日期') {
            obj[userRelations[key]] = formatDate(item[key], '/')
            return false
          }
          obj[userRelations[key]] = item[key]
        })
        return obj
        /*    return {
          timeOfEntry: item['入职日期'],
          username: item['姓名'],
          workNumber: item['工号'],
          mobile: item['手机号'],
          correctionTime: item['转正日期']
        }*/
      })
      await batchImportEmployees(excelArr)
      this.$message.success('上传成功')
      this.$router.push('/employees')
    }
  }
}
</script>

<style scoped lang='scss'>

</style>
